home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / stevie.arc / FILEIO.C < prev    next >
Text File  |  1990-01-10  |  4KB  |  215 lines

  1. /*
  2.  * STEVIE - ST Editor for VI Enthusiasts   ...Tim Thompson...twitch!tjt...
  3.  *
  4.  * Extensive modifications by:  Tony Andrews       onecom!wldrdg!tony
  5.  * Turbo C 1.5 port by: Denny Muscatelli 061988
  6.  */
  7.  
  8. #include "stevie.h"
  9.  
  10. void
  11. filemess(s)
  12. char *s;
  13. {
  14.     smsg("\"%s\" %s", (Filename == NULL) ? "" : Filename, s);
  15. }
  16.  
  17. void
  18. renum()
  19. {
  20.     LPTR    *p;
  21.     unsigned int l = 0;
  22.  
  23.     for (p = Filemem; p != NULL ;p = nextline(p), l += LINEINC)
  24.         p->linep->num = l;
  25.  
  26.     Fileend->linep->num = 0xffff;
  27. }
  28.  
  29. #ifdef    MEGAMAX
  30. overlay "fileio"
  31. #endif
  32.  
  33. bool_t
  34. readfile(fname,fromp,nochangename)
  35. char    *fname;
  36. LPTR    *fromp;
  37. bool_t    nochangename;    /* if TRUE, don't change the Filename */
  38. {
  39.     FILE    *f, *fopen();
  40.     LINE    *curr;
  41.     char    buff[1024];
  42.     char    *p;
  43.     int    i, c;
  44.     long    nchars;
  45.     int    unprint = 0;
  46.     int    linecnt = 0;
  47.     bool_t    wasempty = bufempty();
  48.  
  49.     curr = fromp->linep;
  50.  
  51.     if ( ! nochangename )
  52.         Filename = strsave(fname);
  53.  
  54.     if ( (f=fopen(fname,"r")) == NULL )
  55.         return TRUE;
  56.  
  57.     filemess("");
  58.  
  59.     for (i=nchars=0; (c=getc(f)) != EOF ;nchars++) {
  60.         if (c >= 0x80) {
  61.             c -= 0x80;
  62.             unprint++;
  63.         }
  64.  
  65.         /*
  66.          * Nulls are special, so they can't show up in the file.
  67.          * We should count nulls seperate from other nasties, but
  68.          * this is okay for now.
  69.          */
  70.         if (c == NUL) {
  71.             unprint++;
  72.             continue;
  73.         }
  74.  
  75.         if (c == '\n') {    /* process the completed line */
  76.             int    len;
  77.             LINE    *lp;
  78.  
  79.             buff[i] = '\0';
  80.             len = strlen(buff) + 1;
  81.             if ((lp = newline(len)) == NULL)
  82.                 exit(1);
  83.  
  84.             strcpy(lp->s, buff);
  85.  
  86.             curr->next->prev = lp;    /* new line to next one */
  87.             lp->next = curr->next;
  88.  
  89.             curr->next = lp;    /* new line to prior one */
  90.             lp->prev = curr;
  91.  
  92.             curr = lp;        /* new line becomes current */
  93.             i = 0;
  94.             linecnt++;
  95.         }
  96.         else
  97.             buff[i++] = c;
  98.     }
  99.     fclose(f);
  100.  
  101.     /*
  102.      * If the buffer was empty when we started, we have to go back
  103.      * and remove the "dummy" line at Filemem and patch up the ptrs.
  104.      */
  105.     if (wasempty) {
  106.         LINE    *dummy = Filemem->linep;    /* dummy line ptr */
  107.  
  108.         free(dummy->s);                /* free string space */
  109.         Filemem->linep = Filemem->linep->next;
  110.         free(dummy);                /* free LINE struct */
  111.         Filemem->linep->prev = NULL;
  112.  
  113.         Curschar->linep = Filemem->linep;
  114.         Topchar->linep  = Filemem->linep;
  115.     }
  116.  
  117.     if ( unprint > 0 )
  118.         p="\"%s\" %d lines, %ld characters (%d un-printable))";
  119.     else
  120.         p="\"%s\" %d lines, %ld characters";
  121.  
  122.     sprintf(buff, p, fname, linecnt, nchars, unprint);
  123.     msg(buff);
  124.     renum();
  125.     return FALSE;
  126. }
  127.  
  128.  
  129. /*
  130.  * writeit - write to file 'fname' lines 'start' through 'end'
  131.  *
  132.  * If either 'start' or 'end' contain null line pointers, the default
  133.  * is to use the start or end of the file respectively.
  134.  */
  135. bool_t
  136. writeit(fname, start, end)
  137. char    *fname;
  138. LPTR    *start, *end;
  139. {
  140.     FILE    *f, *fopen();
  141.     FILE    *fopenb();        /* open in binary mode, where needed */
  142.     char    buff[80];
  143.     char    backup[16], *s;
  144.     long    nchars;
  145.     int    lines;
  146.     LPTR    *p;
  147.  
  148.     sprintf(buff, "\"%s\"", fname);
  149.     msg(buff);
  150.  
  151.     /*
  152.      * Form the backup file name - change foo.* to foo.bak
  153.      */
  154.     strcpy(backup, fname);
  155.     for (s = backup; *s && *s != '.' ;s++)
  156.         ;
  157.     *s = NUL;
  158.     strcat(backup, ".bak");
  159.  
  160.     /*
  161.      * Delete any existing backup and move the current version
  162.      * to the backup. For safety, we don't remove the backup
  163.      * until the write has finished successfully. And if the
  164.      * 'backup' option is set, leave it around.
  165.      */
  166.     rename(fname, backup);
  167.  
  168.  
  169.     f = P(P_CR) ? fopen(fname, "w") : fopenb(fname, "w");
  170.  
  171.     if ( f == NULL ) {
  172.         emsg("Can't open file for writing!");
  173.         return FALSE;
  174.     }
  175.  
  176.     /*
  177.      * If we were given a bound, start there. Otherwise just
  178.      * start at the beginning of the file.
  179.      */
  180.     if (start == NULL || start->linep == NULL)
  181.         p = Filemem;
  182.     else
  183.         p = start;
  184.  
  185.     lines = nchars = 0;
  186.     do {
  187.         fprintf(f, "%s\n", p->linep->s);
  188.         nchars += strlen(p->linep->s) + 1;
  189.         lines++;
  190.  
  191.         /*
  192.          * If we were given an upper bound, and we just did that
  193.          * line, then bag it now.
  194.          */
  195.         if (end != NULL && end->linep != NULL) {
  196.             if (end->linep == p->linep)
  197.                 break;
  198.         }
  199.  
  200.     } while ((p = nextline(p)) != NULL);
  201.  
  202.     fclose(f);
  203.     sprintf(buff,"\"%s\" %d lines, %ld characters", fname, lines, nchars);
  204.     msg(buff);
  205.     UNCHANGED;
  206.  
  207.     /*
  208.      * Remove the backup unless they want it left around
  209.      */
  210.     if (!P(P_BK))
  211.         remove(backup);
  212.  
  213.     return TRUE;
  214. }
  215.